package com.zretc.blog.controller;

import com.zretc.api.pojo.Blog;
import com.zretc.blog.service.BlogService;
import com.zretc.blog.service.CommentService;
import com.zretc.common.entity.R;
import com.zretc.common.entity.RespResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/blog")
public class BlogController {
    @Autowired
    private BlogService blogService;
    @Autowired
    private CommentService commentService;
//    @Autowired
//    RedisTemplate redisTemplate;
    @RequestMapping("/addBlog")
    public RespResult addBlog(@RequestBody Blog blog){
         blogService.addBlog(blog);
         return R.success("添加博文成功");
    }

    @RequestMapping("/updateBlog")
    public RespResult updateBlog(@RequestBody Blog blog){
        blogService.updateBlog(blog);
        return R.success("修改博文成功");
    }

    @GetMapping("/getBlogById")
    public RespResult getBlogById(Integer id){
        Blog blog = blogService.getBlogById(id);
        //System.out.println(blog);
        return R.success(blog);
    }

    @GetMapping("/getBlogByUser")
    public RespResult getBlogByUser(Integer bloggerId){
        List<Blog> list = blogService.getBlogByUser(bloggerId);
        //redisTemplate.opsForValue().set("blogList",list);
        //Object o = redisTemplate.opsForValue().get("blogList");
        //System.out.println(o);
        return R.success(list);
    }

    @GetMapping("/getBlogByTitle")
    public RespResult getBlogByTitle(String info){
        ArrayList<Blog> list = blogService.getBlogByTitle(info);
        return R.success(list);
    }
    @GetMapping("/getBlogByContent")
    public RespResult getBlogByContent(String info){
        ArrayList<Blog> list = blogService.getBlogByContent(info);
        return R.success(list);
    }

    @GetMapping("/getBlogByBlogger")
    public RespResult getBlogByBlogger(String info){
        ArrayList<Blog> list = blogService.getBlogByBlogger(info);
        return R.success(list);
    }

    @GetMapping("/getBlogByStatus")
    public RespResult getBlogByStatus(Integer status){
        ArrayList<Blog> list = blogService.getBlogByStatus(status);
        return R.success(list);
    }
    @GetMapping("/getBlogByLikes")
    public RespResult getBlogByLikes(){
        ArrayList<Blog> list = blogService.getBlogByLikes();
        return R.success(list);
    }
//    @GetMapping("/getTotalLikes")
//    public RespResult getTotalLikes(Integer bloggerId){
//        Integer totalLikes = blogService.getTotalLikes(bloggerId);
//        return R.success(totalLikes);
//    }
    @RequestMapping("/setBlogStatus")
    public RespResult setBlogStatus(Blog blog){
        blogService.setBlogStatus(blog);
        return R.success("修改博文状态成功");
    }

    @GetMapping("/deleteBlog")
    public RespResult deleteBlog(Integer id){
        blogService.deleteBlog(id);
        return R.success("删除博文成功");
    }

    @GetMapping("/likeBlog")
    public RespResult likeBlog(Integer id){
        blogService.likeBlog(id);
        return R.success("点赞博文成功");
    }

    /*@RequestMapping("/delete")
    public RespResult deleteBlog(){
        return R.success("delete成功");
    }*/

    @PreAuthorize("hasAnyAuthority('system:dept:list')")
    @RequestMapping("/m1")
    public RespResult m1(){
        return R.success("m1成功");
    }
    @PreAuthorize("hasAnyAuthority('system:job:list')")
    @RequestMapping("/m2")
    public RespResult m2(){
        return R.success("m2成功");
    }

    @RequestMapping("/m3")
    public RespResult m3(){
        return R.success("m3成功");
    }
}
